/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcdaos.UserDao;
import jdbcpojos.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Akcje zwiazane z zarzadzaniem uzytkownikiem
 * @author piotrek
 */
@Controller
@RequestMapping(value="/jdbc")
public class UserController {

	private UserDao userDao;

	@Autowired
    public UserController( UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user", method=RequestMethod.GET)
    protected String showUserJdbc( @PathVariable("id") String id,
            Model model) throws Exception {
		
		model.addAttribute( "id", id );

		Customer customer = userDao.getUser( Integer.parseInt( id ) );

		model.addAttribute( "user", customer );

		return "user";
    }

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user/update", method=RequestMethod.GET)
    protected String updateUserJdbc( @PathVariable("id") String id,
            Model model) throws Exception {

		Customer c = userDao.getUser( Integer.parseInt(id) );
		c.setLogin("zmieniony");
		c.setStreet("inna");
		userDao.updateUser( c );

		return "success";
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/user/delete", method=RequestMethod.GET)
    protected String deleteUserJdbc( @PathVariable("id") String id,
            Model model) throws Exception {

		userDao.deleteUser( Integer.parseInt(id) );

		return "success";
	}


	@RequestMapping(value="/user/add")
	protected String saveUser(HttpServletRequest request,
            HttpServletResponse response){

		Customer c = new Customer();
//		c.setLogin( (String)request.getParameter("login") );
//		c.setPasswd( (String)request.getParameter("passwd") );
//		c.setCity( (String)request.getParameter("city") );
//		c.setFlat_number( Integer.parseInt( request.getParameter("flat_number") ) );
//		c.setName( (String)request.getParameter("name") );
//		c.setPostal( (String)request.getParameter("postal") );
//		c.setStreet( (String)request.getParameter("street") );
//		c.setStreet_number( Integer.parseInt( request.getParameter("street_number") ) );
//		c.setSurname( (String)request.getParameter("surname") );

		c.setLogin( "login" );
		c.setPasswd( "haslo" );
		c.setCity( "miasto" );
		c.setFlat_number( 12 );
		c.setName( "imie" );
		c.setPostal( "12-345" );
		c.setStreet( "ulica" );
		c.setStreet_number( 12 );
		c.setSurname( "nazwisko" );

		userDao.addUser( c );

		return "success";

	}



}